package com.dongdongshop.controller;

import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Description
 * @ClassName TestSmsController
 * @Author RC
 * @date 2020.12.19 10:57
 */
@Controller
public class TestSmsController {

    @RequestMapping("testSendSms")
    @ResponseBody
    public String sendSms(String phone){
        YunpianClient clnt = null;
        try {
            //初始化clnt,使用单例方式
             clnt = new YunpianClient("e71525450d93acdd30c1f6a5341d48a4").init();
             //随机密码
            String code = RandomStringUtils.randomNumeric(6);
            //发送短信API
            Map<String, String> param = clnt.newParam(2);
            param.put(YunpianClient.MOBILE, phone);
            param.put(YunpianClient.TEXT, "【任畅】亲爱的东东商城用户你好,您的验证码是"+code+"。有效时间五分钟,如非本人操作，请忽略本短信。");
            Result<SmsSingleSend> r = clnt.sms().single_send(param);
            //获取返回结果，返回码:r.getCode(),返回码描述:r.getMsg(),API结果:r.getData(),其他说明:r.getDetail(),调用异常:r.getThrowable()

            //账户:clnt.user().* 签名:clnt.sign().* 模版:clnt.tpl().* 短信:clnt.sms().* 语音:clnt.voice().* 流量:clnt.flow().* 隐私通话:clnt.call().*
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(clnt != null && !"".equals(clnt) ){
                //释放clnt
                clnt.close();
            }
        }
        return "send ok";


    }
}
